﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class BillDAO:IData
 	{
		public BillDAO(String myconnecString) : base(myconnecString) { }

		public  int OnSubmit(ref Bill item, int t) {
            SqlParameter p = new SqlParameter();
            if( t == 0)
                p.Direction = ParameterDirection.Output;
            p.ParameterName = "@" + Bill.c_id;
            p.Value = item.id;
            Object obj = item.id;
            int i =
			  connec.ExecuteNonQuery("sp_Bill", CommandType.StoredProcedure,p,ref obj,
                 new SqlParameter("@flag", t), new SqlParameter("@" + Bill.c_customersId, item.customersId),
				 new SqlParameter("@" + Bill.c_address, item.address), new SqlParameter("@" + Bill.c_create_on, item.create_on),
				 new SqlParameter("@" + Bill.c_cardId, item.cardId), new SqlParameter("@" + Bill.c_total, item.total),
				 new SqlParameter("@" + Bill.c_discount, item.discount), new SqlParameter("@" + Bill.c_payment, item.payment),
				 new SqlParameter("@" + Bill.c_arrears, item.arrears), new SqlParameter("@" + Bill.c_credit, item.credit),
				 new SqlParameter("@" + Bill.c_status, item.status), new SqlParameter("@" + Bill.c_orderId, item.orderId),
				 new SqlParameter("@" + Bill.c_isActive, item.isActive));
            item.id = (long)obj;
            return i;
		}

		public  Bill GetItem(IDataReader dr)
		{
			Bill obj = new Bill();
			obj.id = Convert.ToInt64(dr[Bill.c_id]);
			obj.customersId = Convert.ToInt64(dr[Bill.c_customersId]);
			obj.address = Convert.ToString(dr[Bill.c_address]);
			obj.create_on = Convert.ToDateTime(dr[Bill.c_create_on]);
			obj.cardId = Convert.ToInt64(dr[Bill.c_cardId]);
			obj.total = Convert.ToDecimal(dr[Bill.c_total]);
			obj.discount = Convert.ToDecimal(dr[Bill.c_discount]);
			obj.payment = Convert.ToDecimal(dr[Bill.c_payment]);
			obj.arrears = Convert.ToDecimal(dr[Bill.c_arrears]);
			obj.credit = Convert.ToString(dr[Bill.c_credit]);
			obj.status = Convert.ToInt32(dr[Bill.c_status]);
			obj.orderId = Convert.ToInt32(dr[Bill.c_orderId]);
			obj.isActive = Convert.ToInt32(dr[Bill.c_isActive]);
			return obj;
		}

        private List<Bill> GetAll(String strSQL, CommandType commandtype)
        {
            List<Bill> list = new List<Bill>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = commandtype;
            command.CommandText = strSQL;
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }

		public  List<Bill> GetAll()
		{
			return GetAll("select * from " + Bill.b_Bill, CommandType.Text);
		}

		public  Bill GetItemById(object id)
		{
			 return this.GetAll().FirstOrDefault(x => x.id == (long)id);
		}

	}

 }
